package com.whitedust.top70;

import com.whitedust.structure.TreeNode;

import java.util.logging.Level;

/**
 *
 * @author A
 * @since 2025/11/11 12:55
 */

public class Top70 {
    public boolean isBalanced(TreeNode root) {
        return getHeightDiff(root) != -1;
    }

    public int getHeightDiff(TreeNode root) {
        if (root == null) return 0;
        int left = getHeightDiff(root.left);
        if (left == -1) return -1;
        int right = getHeightDiff(root.right);
        if (right == -1) return -1;
        return Math.abs(left - right) <= 1 ? Math.max(left, right) + 1 : -1;
    }
}
